package com.jhz.speedy.common.base;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * FragmentPagerAdapter 基类
 *
 * Created by 均合 on 2017/1/19.
 */

public class BaseFragmentAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public BaseFragmentAdapter(FragmentManager fragmentManager, List<Fragment> newsFragmentList) {
        super(fragmentManager);
        updataList(newsFragmentList);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public long getItemId(int position) {
        // 获取当前数据的hashCode
        int hashCode = fragments.get(position).hashCode();
        return hashCode;
    }

    public void updataList(List<Fragment> list){
        if(list == null)
            list = new ArrayList<>();
        fragments = list;
        notifyDataSetChanged();
    }

    public void addFragment(Fragment fragment){
        this.fragments.add(fragment);
        notifyDataSetChanged();
    }

    public void remove(int index){
        fragments.remove(index);
        notifyDataSetChanged();
    }
}
